<template>
  <div 
    class="container-component"
    :style="containerStyles"
  >
    <!-- 容器标题 -->
    <div v-if="showTitle" class="container-title" :style="titleStyles">
      {{ component.title || '容器' }}
      <div v-if="!props.isPreview && component.children && component.children.length" class="title-actions">
        <el-button 
          size="mini" 
          icon="el-icon-plus" 
          @click.stop="addChildComponent"
          title="添加子组件"
        />
      </div>
    </div>
    
    <!-- 容器内容区 -->
    <div class="container-content" :style="contentStyles">
      <!-- 子组件 -->
      <template v-for="(child, index) in component.children || []" :key="child.id">
        <component-wrapper 
          :component="child"
          :index="index"
          :snap-to-grid="snapToGrid"
          :grid-size="gridSize"
          :is-selected="selectedComponentId === child.id"
          :is-preview="props.isPreview"
          @select="handleChildSelect"
          @update="handleChildUpdate"
          @remove="handleChildRemove"
          @z-index-up="handleZIndexUp(index)"
          @z-index-down="handleZIndexDown(index)"
        />
      </template>
      
      <!-- 空状态提示 -->
      <div v-if="isEmpty && !props.isPreview" class="empty-state">
        <el-icon class="empty-icon"><Plus /></el-icon>
        <p class="empty-text">点击添加子组件</p>
        <el-button 
          size="small" 
          type="primary" 
          @click="addChildComponent"
        >
          <Plus class="mr-1" />添加组件
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { defineProps, ref, computed, inject } from 'vue';
import { Plus } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
// import ComponentWrapper from './ComponentWrapper.vue';
import { v4 as uuidv4 } from 'uuid';

export default {
  name: 'ContainerComponent',
  components: {
   
    Plus
  },
  props: {
    component: {
      type: Object,
      required: true,
      description: '容器组件配置'
    },
    isPreview: {
      type: Boolean,
      default: false,
      description: '是否预览模式'
    }
  },
  setup(props) {
    // 从父组件注入的属性和方法
    const selectedComponentId = inject('selectedComponentId', ref(null));
    const snapToGrid = inject('snapToGrid', ref(true));
    const gridSize = inject('gridSize', ref(20));
    const handleComponentSelect = inject('handleComponentSelect', () => {});
    const handleComponentUpdate = inject('handleComponentUpdate', () => {});
    const handleComponentRemove = inject('handleComponentRemove', () => {});
    
    // 容器是否为空
    const isEmpty = computed(() => {
      return !(props.component.children && props.component.children.length > 0);
    });
    
    // 是否显示标题
    const showTitle = computed(() => {
      return props.component.showTitle !== false;
    });
    
    // 容器样式
    const containerStyles = computed(() => ({
      width: '100%',
      height: '100%',
      backgroundColor: props.component.backgroundColor || '#f5f7fa',
      borderRadius: props.component.borderRadius ? `${props.component.borderRadius}px` : '4px',
      border: props.component.borderWidth ? `${props.component.borderWidth}px solid ${props.component.borderColor || '#e4e7ed'}` : '1px solid #e4e7ed',
      boxShadow: props.component.boxShadow || 'none',
      overflow: 'hidden',
      boxSizing: 'border-box'
    }));
    
    // 标题样式
    const titleStyles = computed(() => ({
      padding: '8px 16px',
      backgroundColor: props.component.titleBackground || '#eef2f7',
      borderBottom: props.component.showTitleDivider !== false ? '1px solid #e4e7ed' : 'none',
      fontSize: props.component.titleFontSize || '14px',
      color: props.component.titleColor || '#303133',
      fontWeight: props.component.titleFontWeight || '500',
      display: 'flex',
      justifyContent: 'space-between',
      alignItems: 'center'
    }));
    
    // 内容区样式
    const contentStyles = computed(() => ({
      width: '100%',
      height: showTitle.value ? `calc(100% - ${props.component.titleHeight || 40}px)` : '100%',
      padding: props.component.padding || '16px',
      boxSizing: 'border-box',
      overflow: props.component.overflow || 'hidden'
    }));
    
    // 添加子组件
    const addChildComponent = () => {
      if (props.isPreview) return;
      
      // 初始化children数组
      if (!props.component.children) {
        props.component.children = [];
      }
      
      // 添加一个默认文本组件
      const newComponent = {
        id: uuidv4(),
        type: 'text',
        label: '文本',
        x: 20,
        y: 20,
        width: 200,
        height: 50,
        content: '子组件文本',
        fontSize: 14,
        color: '#333333',
        textAlign: 'center',
        backgroundColor: 'transparent',
        zIndex: props.component.children.length + 1
      };
      
      props.component.children.push(newComponent);
      ElMessage.success('已添加子组件');
    };
    
    // 处理子组件选择
    const handleChildSelect = (id, event) => {
      event.stopPropagation();
      handleComponentSelect(id, event);
    };
    
    // 处理子组件更新
    const handleChildUpdate = (id, childProps) => {
      const index = props.component.children.findIndex(item => item.id === id);
      if (index !== -1) {
        props.component.children[index] = {
          ...props.component.children[index],
          ...childProps
        };
      }
    };
    
    // 处理子组件删除
    const handleChildRemove = (id) => {
      props.component.children = props.component.children.filter(item => item.id !== id);
      if (selectedComponentId.value === id) {
        selectedComponentId.value = null;
      }
    };
    
    // 调整子组件层级（上移）
    const handleZIndexUp = (index) => {
      if (!props.component.children || index >= props.component.children.length - 1) return;
      
      // 交换位置
      const temp = props.component.children[index];
      props.component.children[index] = props.component.children[index + 1];
      props.component.children[index + 1] = temp;
      
      // 更新z-index
      props.component.children[index].zIndex = index + 1;
      props.component.children[index + 1].zIndex = index + 2;
    };
    
    // 调整子组件层级（下移）
    const handleZIndexDown = (index) => {
      if (!props.component.children || index <= 0) return;
      
      // 交换位置
      const temp = props.component.children[index];
      props.component.children[index] = props.component.children[index - 1];
      props.component.children[index - 1] = temp;
      
      // 更新z-index
      props.component.children[index].zIndex = index + 1;
      props.component.children[index - 1].zIndex = index;
    };
    
    return {
      selectedComponentId,
      snapToGrid,
      gridSize,
      isEmpty,
      showTitle,
      containerStyles,
      titleStyles,
      contentStyles,
      addChildComponent,
      handleChildSelect,
      handleChildUpdate,
      handleChildRemove,
      handleZIndexUp,
      handleZIndexDown
    };
  }
};
</script>

<style scoped>
.container-component {
  position: relative;
}

.container-content {
  position: relative;
}

.empty-state {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #909399;
  background-color: rgba(0, 0, 0, 0.01);
}

.empty-icon {
  font-size: 24px;
  margin-bottom: 8px;
}

.empty-text {
  margin-bottom: 16px;
  font-size: 14px;
}

.title-actions {
  display: flex;
  gap: 4px;
}
</style>
    